package com.wm.task1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        int port = 8888;
        try {
            ServerSocket serverSocket = new ServerSocket(port);//在port这个端口创建服务器
            while (true){
                System.out.println("等待链接.....");
                Socket socket = serverSocket.accept();  //等待客户端链接服务器
                System.out.println("链接成功!!");
                OutputStream os = socket.getOutputStream();//以字节流向客户端发送数据
                OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");//将os的字节流转换成字符流
                File file = new File("D:/java/study/test/download/news.txt");
                FileReader fr = new FileReader(file);
                while (true) {
                    int r = fr.read();

                    if (r == -1) {
                        break;
                    }
                    System.out.print(r);
                    osw.write(r);
                }

                osw.flush();
                fr.close();
                osw.close();
                os.close();
                socket.close();
            }



        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
